<?php
/**
 * @see Epic_Validate_Element_Abstract
 */
require_once 'Epic/Validate/Element/Abstract.php';

class Epic_Validate_Element
{
	/**
	 *  Epic_Validate_Element_Interface object
	 * @var Epic_Validate_Element_Interface object
	 */
    private $_validators = array();
    
    /**
     * Error message
     * @var errors display
     */
    private $_errors = array();
   
    /**
     * Add a validator
     * 
     * @param Epic_Validate_Element_Interface $validator
     * @return Epic_Validate_Element
     */
    public function addValidator(Epic_Validate_Element_Interface $validator)
    {
        $this->_validators[] = $validator;
        return $this;
    }
   
    /**
     * Get all the validators object
     * 
     * @return array|null
     */
    public function getValidators()
    {
        return !empty($this->_validators) ? $this->_validators : null;
    }

    /**
     * Validate inputted data
     * 
     * @return boolean 
     */
    public function isValid()
    {
        $validators = $this->getValidators();
        if (null !== $validators) {
            foreach ($validators as $validator) {
                if (!$validator->isValid()) {
                    $this->_errors[] = $validator->getErrorMessage();
                }
            } 
        }
        return empty($this->_errors) ? true : false;
    }
    
    /**
     * Get validation errors as an array
     * @return array 
     */
    public function getErrors()
    {
        return $this->_errors;
    }

    /**
     * Get validation errors as a string
     * @return string 
     */
    public function getErrorString()
    {
        $errors = $this->getErrors();
        return !empty($errors) ? implode('', $errors) : '';
    }

    /**
     * Clear state of the form helper
     */
    public function clear()
    {
        $this->_validators = array();
        $this->_errors = array();
    }   
}  